Crate nom_locate [] [src]

nom_locate, a special input type to locate tokens

The source code is available on Github

How to use it

The explanations are given in the README of the Github repository. You may also consult the FAQ.

#[macro_use]
extern crate nom;
#[macro_use(position)]
extern crate nom_locate;

use nom_locate::LocatedSpan;
type Span<'a> = LocatedSpan<&'a str>;

struct Token<'a> {
    pub position: Span<'a>,
    pub foo: String,
    pub bar: String,
}

named!(parse_foobar( Span ) -> Token, do_parse!(
    take_until!("foo") >>
    position: position!() >>
    foo: tag!("foo") >>
    bar: tag!("bar") >>
    (Token {
        position: position,
        foo: foo.to_string(),
        bar: bar.to_string()
    })
));

fn main () {
    let input = Span::new("Lorem ipsum \n foobar");
    let output = parse_foobar(input);
    let position = output.unwrap().1.position;
    assert_eq!(position, Span {
        offset: 14,
        line: 2,
        fragment: ""
    });
    assert_eq!(position.get_column(), 2);
}

Macros

impl_compare

Implement nom::Compare for a specific fragment type.

impl_find_token

Implement nom::FindToken for a specific token type.

impl_input_iter

Implement nom::InputIter for a specific fragment type

impl_offset

Implement nom::Offset for a specific fragment type.

impl_slice_range

Implement nom::Slice for a specific fragment type and range type.

impl_slice_ranges

Implement nom::Slice for a specific fragment type and for these types of range: * Range<usize> * RangeTo<usize> * RangeFrom<usize> * RangeFull

position

Capture the position of the current fragment

Structs

LocatedSpan

A LocatedSpan is a set of meta information about the location of a token.